package com.ssm.okex;

import com.google.gson.Gson;

import java.util.*;
import java.net.*;
import java.io.*;

public class OKexBTCPrice {
    public static void main(String[] args) throws Exception {
        
        String url = "https://www.okex.com/api/v1/future_ticker.do?symbol=btc_usd";

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", "Mozilla/5.0");

        int responseCode = con.getResponseCode();

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        String jsonStr = response.toString();

        Map<String, Object> map = new HashMap<String, Object>();
        map = new Gson().fromJson(jsonStr, map.getClass());

        double lastPrice = (double) map.get("ticker.last");
        System.out.println("BTC合约最新价格：$" + lastPrice);
    }
}